package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;


    @Override //分页查询护理计划
    public PageResponse<NursingPlanVo> selectByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize); //分页插件
        Page<NursingPlanVo> planVoPage = (Page<NursingPlanVo>)nursingPlanMapper.selectByPage(name,status);

        return PageResponse.of(planVoPage,NursingPlanVo.class);
    }

    @Override  // 需要向两张表插入数据：护理计划  中间表
    public void add(NursingPlanDto nursingPlanDto) {
        // 一、新增护理计划
        // 1、转实体类
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);

        // 2、考虑默认值
        nursingPlan.setStatus(1);
        nursingPlanMapper.add(nursingPlan);  //新增护理计划后需要返回对象的id

        // 二、新增中间表 使用批量新增

        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        // 转成需要插入的数据ProjectPlanDetailDTO-->NursingProjectPlan
        List<NursingProjectPlan> nursingProjectPlans = BeanUtil.copyToList(projectPlans, NursingProjectPlan.class);
        for (NursingProjectPlan nursingProjectPlan : nursingProjectPlans) {
            nursingProjectPlan.setPlanId(nursingPlan.getId());
        }

        // 批量新增
        nursingProjectPlanMapper.addBatch(nursingProjectPlans);


    }

    @Override
    public NursingPlanVo getById(Long id) {
        // 查询两个表的数据  护理计划  中间表
        NursingPlanVo nursingPlanVo =  nursingPlanMapper.getById(id);

        List<NursingProjectPlanVO> projectPlanVOS =  nursingProjectPlanMapper.getByPlanId(id);

        nursingPlanVo.setProjectPlans(projectPlanVOS);
        return nursingPlanVo;
    }

    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        // 1、修改护理计划表
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.update(nursingPlan);

        // 2、根据计划id先删除中间表的数据
        nursingProjectPlanMapper.deleteByPlanId(nursingPlanDto.getId());
        // 3、批量新增

        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        // 转成需要插入的数据ProjectPlanDetailDTO-->NursingProjectPlan
        List<NursingProjectPlan> nursingProjectPlans = BeanUtil.copyToList(projectPlans, NursingProjectPlan.class);
        for (NursingProjectPlan nursingProjectPlan : nursingProjectPlans) {
            nursingProjectPlan.setPlanId(nursingPlan.getId());
        }

        // 批量新增
        nursingProjectPlanMapper.addBatch(nursingProjectPlans);

    }

    @Override
    public void delete(Long id) {
        // 删除护理计划
        nursingPlanMapper.deleteById(id);

                // 删除中间表
        nursingProjectPlanMapper.deleteByPlanId(id);
    }

}
